package ebook.datastructure.down.Chap06.anagram;// anagram.java
// creates anagrams
// to run this program: C>java AnagramApp

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

////////////////////////////////////////////////////////////////
class AnagramApp {

    static int size;

    static int count;

    static char[] arrChar;

    static List<String> resultList = new ArrayList();


    //-----------------------------------------------------------
    public static void main(String[] args) throws IOException {
        System.out.print("Enter a word: ");    // get word
        String input = getString();
        size = input.length();                 // find its size
        count = 0;
        arrChar = new char[size];
        for (int j = 0; j < size; j++) {            // put it in array
            arrChar[j] = input.charAt(j);
        }
        doAnagram(size);                       // anagram it
        System.out.println(resultList);
    }  // end main()


    //-----------------------------------------------------------
    public static void doAnagram(int newSize) {
        if (newSize == 1) {                    // if too small,
            return;                           // go no further
        }

        for (int j = 0; j < newSize; j++) {        // for each position,
            doAnagram(newSize - 1);             // anagram remaining
            if (newSize == 2) {                  // if innermost,
                displayWord();                 // display it
                resultList.add(new String(arrChar));
            }
            rotate(newSize);                  // rotate word
        }
    }


    //-----------------------------------------------------------
    // rotate left all chars from position to end
    public static void rotate(int newSize) {
        int j;
        int position = size - newSize;
        char temp = arrChar[position];       // save first letter
        for (j = position + 1; j < size; j++) {      // shift others left
            arrChar[j - 1] = arrChar[j];
        }
        arrChar[j - 1] = temp;                 // put first on right
    }


    //-----------------------------------------------------------
    public static void displayWord() {
//      if(count < 99)
//         System.out.print(" ");
//      if(count < 9)
//         System.out.print(" ");
//          System.out.print(++count + " ");
        System.out.print(String.format("%-4s", ++count + " "));
        for (int j = 0; j < size; j++)
            System.out.print(arrChar[j]);
        System.out.print("   ");
        System.out.flush();
        if (count % 6 == 0)
            System.out.println("");
    }


    //-----------------------------------------------------------
    public static String getString() throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        return s;
    }
    //-----------------------------------------------------------
}  // end class AnagramApp
////////////////////////////////////////////////////////////////
